package com.api.zszsj2.biz;

import com.engine.kq.biz.KQBalanceOfLeaveBiz;
import com.engine.kq.biz.KQLeaveRulesComInfo;
import com.engine.kq.biz.KQUsageHistoryBiz;
import com.engine.kq.entity.KQBalanceOfLeaveEntity;
import com.engine.kq.entity.KQUsageHistoryEntity;
import weaver.conn.RecordSet;
import weaver.general.Util;

import java.util.*;

public class KQBalanceOfLeaveBizCustomize extends KQBalanceOfLeaveBiz {

    /**
     * 获取指定机构下的人员的假期余额
     * 复制自 com.engine.kq.biz.KQBalanceOfLeaveBiz 同名function
     * @param organizationType 指定机构类型：0-总部、1-分部、2-部门、3-热力资源
     * @param organizationIds  指定机构的ID，用英文逗号分隔
     * @param ruleId           假期类型的ID
     * @param belongYear       所属年份
     * @return
     */
    public static Map<String, Object> getBalanceEntities(int organizationType, String organizationIds, String ruleId, String belongYear) {
        Map<String, Object> balanceEntities = new HashMap<String, Object>();
        try {
            KQBalanceOfLeaveEntity balanceEntity = null;

            //假期类型缓存类
            KQLeaveRulesComInfo rulesComInfo = new KQLeaveRulesComInfo();
            //最小请假单位：1-按天请假、2-按半天请假、3-按小时请假、4-按整天请假
            String minimumUnit = rulesComInfo.getMinimumUnit(ruleId);

            RecordSet recordSet = new RecordSet();
            String sql = "select * from kq_BalanceOfLeave where leaveRulesId=" + ruleId + " and belongYear=" + belongYear;
            if (organizationType == 1) {
                sql += " and resourceId in (select id from HrmResource where subCompanyId1 in (" + organizationIds + "))";
            } else if (organizationType == 2) {
                sql += " and resourceId in (select id from HrmResource where departmentId in (" + organizationIds + "))";
            } else if (organizationType == 3) {
                sql += " and resourceId in (" + organizationIds + ")";
            }
            recordSet.executeQuery(sql);
            while (recordSet.next()) {
                int id = recordSet.getInt("id");
                int resourceId = recordSet.getInt("resourceId");
                int leaveRuleId = recordSet.getInt("leaveRulesId");
                double baseAmount = Util.getDoubleValue(recordSet.getString("baseAmount"), 0.00);
                double extraAmount = Util.getDoubleValue(recordSet.getString("extraAmount"), 0.00);
                double usedAmount = Util.getDoubleValue(recordSet.getString("usedAmount"), 0.00);
                double baseAmount2 = Util.getDoubleValue(recordSet.getString("baseAmount2"), 0.00);
                double extraAmount2 = Util.getDoubleValue(recordSet.getString("extraAmount2"), 0.00);
                double usedAmount2 = Util.getDoubleValue(recordSet.getString("usedAmount2"), 0.00);

                balanceEntity = new KQBalanceOfLeaveEntity();
                balanceEntity.setId(id);
                balanceEntity.setResourceId(resourceId);
                balanceEntity.setBelongYear(belongYear);
                balanceEntity.setLeaveRulesId(leaveRuleId);
                balanceEntity.setBaseAmount(baseAmount);
                balanceEntity.setExtraAmount(extraAmount);
                balanceEntity.setUsedAmount(usedAmount);
                balanceEntity.setBaseAmount2(baseAmount2);
                balanceEntity.setExtraAmount2(extraAmount2);
                balanceEntity.setUsedAmount2(usedAmount2);
                balanceEntity.setMinimumUnit(Util.getIntValue(minimumUnit));

                balanceEntities.put("" + resourceId, balanceEntity);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return balanceEntities;
    }

    /**
     * 插入或者更新员工假期余额
     * 复制自 com.engine.kq.biz.KQBalanceOfLeaveBiz 同名function
     * @param newBalanceEntities 新假期余额
     * @param oldBalanceEntities 旧假期余额
     * @param operator           操作者
     * @param canUpdate          是否允许更新
     * @return
     */
    public static boolean insertOrUpdateBalance(List<KQBalanceOfLeaveEntity> newBalanceEntities, Map<String, Object> oldBalanceEntities, String operator, boolean canUpdate) {
        /*获取今天的日期、此刻的时间*/
        Calendar today = Calendar.getInstance();
        String currentDate = Util.add0(today.get(Calendar.YEAR), 4) + "-"
                + Util.add0(today.get(Calendar.MONTH) + 1, 2) + "-"
                + Util.add0(today.get(Calendar.DAY_OF_MONTH), 2);
        String currentTime = Util.add0(today.get(Calendar.HOUR_OF_DAY), 2) + ":" +
                Util.add0(today.get(Calendar.MINUTE), 2) + ":" +
                Util.add0(today.get(Calendar.SECOND), 2);

        String insertSql = "insert into kq_balanceOfLeave(leaveRulesId,resourceId,belongYear,expirationdate,baseAmount,extraAmount,usedAmount,baseAmount2,extraAmount2,usedAmount2) " +
                "values(?,?,?,?,?,?,?,?,?,?)";
        List insertList = new ArrayList();
        List insertParamList = new ArrayList();
        String updateSql = "update kq_balanceOfLeave set leaveRulesId=?,resourceId=?,belongYear=?,expirationdate=?,baseAmount=?,baseAmount2=? where id=?";
        List updateList = new ArrayList();
        List updateParamList = new ArrayList();
        List<KQUsageHistoryEntity> usageHistoryEntities = new ArrayList<KQUsageHistoryEntity>();
        KQUsageHistoryEntity usageHistoryEntity = null;
        for (KQBalanceOfLeaveEntity newBalanceEntity : newBalanceEntities) {
            int resourceId = newBalanceEntity.getResourceId();
            KQBalanceOfLeaveEntity oldBalanceEntity = (KQBalanceOfLeaveEntity) oldBalanceEntities.get("" + resourceId);

            String expirationDate = Util.null2String(newBalanceEntity.getExpirationDate());
            if(expirationDate.isEmpty())
                expirationDate = newBalanceEntity.getBelongYear()+"-12-31";//未指定的话，当年内有效

            if (oldBalanceEntity == null || oldBalanceEntity.getId() <= 0) {
                insertList = new ArrayList();
                insertList.add(newBalanceEntity.getLeaveRulesId());
                insertList.add(newBalanceEntity.getResourceId());
                insertList.add(newBalanceEntity.getBelongYear());
                insertList.add(expirationDate);
                insertList.add(newBalanceEntity.getBaseAmount());
                insertList.add(newBalanceEntity.getExtraAmount());
                insertList.add(newBalanceEntity.getUsedAmount());
                insertList.add(newBalanceEntity.getBaseAmount2());
                insertList.add(newBalanceEntity.getExtraAmount2());
                insertList.add(newBalanceEntity.getUsedAmount2());
                insertParamList.add(insertList);

                usageHistoryEntity = new KQUsageHistoryEntity();
                usageHistoryEntity.setLeaveRulesId("" + newBalanceEntity.getLeaveRulesId());
                usageHistoryEntity.setRelatedId("" + newBalanceEntity.getResourceId());
                usageHistoryEntity.setOperator("" + operator);
                usageHistoryEntity.setOperateDate(currentDate);
                usageHistoryEntity.setOperateTime(currentTime);
                usageHistoryEntity.setOperateType("6");
                usageHistoryEntity.setBelongYear(newBalanceEntity.getBelongYear());
                usageHistoryEntity.setNewBaseAmount("" + newBalanceEntity.getBaseAmount());
                usageHistoryEntity.setNewExtraAmount("" + newBalanceEntity.getExtraAmount());
                usageHistoryEntity.setNewUsedAmount("" + newBalanceEntity.getUsedAmount());
                usageHistoryEntity.setNewBaseAmount2("" + newBalanceEntity.getBaseAmount2());
                usageHistoryEntity.setNewExtraAmount2("" + newBalanceEntity.getExtraAmount2());
                usageHistoryEntity.setNewUsedAmount2("" + newBalanceEntity.getUsedAmount2());
                usageHistoryEntity.setOldMinimumUnit("" + newBalanceEntity.getMinimumUnit());
                usageHistoryEntity.setNewMinimumUnit("" + newBalanceEntity.getMinimumUnit());
                usageHistoryEntity.setInsertOrUpdate("insert");
                usageHistoryEntities.add(usageHistoryEntity);
            } else {
                updateList = new ArrayList();
                updateList.add(newBalanceEntity.getLeaveRulesId());
                updateList.add(newBalanceEntity.getResourceId());
                updateList.add(newBalanceEntity.getBelongYear());
                updateList.add(expirationDate);
                updateList.add(newBalanceEntity.getBaseAmount());
                updateList.add(newBalanceEntity.getBaseAmount2());
                updateList.add(oldBalanceEntity.getId());
                updateParamList.add(updateList);

                usageHistoryEntity = new KQUsageHistoryEntity();
                usageHistoryEntity.setLeaveRulesId("" + newBalanceEntity.getLeaveRulesId());
                usageHistoryEntity.setRelatedId("" + newBalanceEntity.getResourceId());
                usageHistoryEntity.setOperator("" + operator);
                usageHistoryEntity.setOperateDate(currentDate);
                usageHistoryEntity.setOperateTime(currentTime);
                usageHistoryEntity.setOperateType("6");
                usageHistoryEntity.setBelongYear(newBalanceEntity.getBelongYear());
                usageHistoryEntity.setOldBaseAmount("" + oldBalanceEntity.getBaseAmount());
                usageHistoryEntity.setNewBaseAmount("" + newBalanceEntity.getBaseAmount());
                usageHistoryEntity.setOldBaseAmount2("" + oldBalanceEntity.getBaseAmount2());
                usageHistoryEntity.setNewBaseAmount2("" + newBalanceEntity.getBaseAmount2());
                usageHistoryEntity.setOldMinimumUnit("" + oldBalanceEntity.getMinimumUnit());
                usageHistoryEntity.setNewMinimumUnit("" + newBalanceEntity.getMinimumUnit());
                usageHistoryEntity.setInsertOrUpdate("update");
                usageHistoryEntities.add(usageHistoryEntity);
            }
        }

        boolean isSuccess = true;
        RecordSet recordSet = new RecordSet();
        /*新增员工假期余额数据 start*/
        if (insertParamList.size() > 0) {
            isSuccess = recordSet.executeBatchSql(insertSql, insertParamList);
        }
        /*新增员工假期余额数据 end*/

        /*更新员工假期余额数据 start*/
        if (updateParamList.size() > 0) {
            isSuccess = recordSet.executeBatchSql(updateSql, updateParamList);
        }
        /*更新员工假期余额数据 end*/

        /*记录员工假期余额变更记录 start*/
        KQUsageHistoryBiz kqUsageHistoryBiz = new KQUsageHistoryBiz();
        if (usageHistoryEntities.size() > 0) {
            isSuccess = kqUsageHistoryBiz.save(usageHistoryEntities);
        }
        /*记录员工假期余额变更记录 end*/

        return isSuccess;
    }
}
